#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int head[N];
int ver[N], W[N], Next[N], tot;
void add(int u, int v, int w) {
  ++tot;
  ver[tot] = v;
  W[tot] = w;
  Next[tot] = head[u];
  head[u] = tot;
}
int vis[N];
int res[N];
vector<int> a;
void dfs(int u, int val) {
  if (vis[u]) {
    if (val != res[u]) {
      cout << -1;
      exit(0);
    }
    return;
  }
  vis[u] = 1;
  res[u] = val;
  a.push_back(u);
  for (int i = head[u]; i; i = Next[i]) {
    int v = ver[i];
    int w = W[i];
    dfs(v, val ^ w);
  }
}
void min_sum() {
  per(i, 30, 0) {
    int cnt = 0;
    for (int j : a) {
      if (res[j] & (1 << i)) cnt++;
    }
    if (cnt * 2 > a.size()) {
      for (int j : a) res[j] ^= 1 << i;
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) {
    int x, y, z;
    cin >> x >> y >> z;
    add(x, y, z);
    add(y, x, z);
  }
  rep(i, 1, n) {
    if (!vis[i]) {
      a.clear();
      dfs(i, 0);
      min_sum();
    }
  }
  rep(i, 1, n) cout << res[i] << ' ';
  return 0;
}